#include <iostream>
#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include "SDL/SDL_ttf.h"
#include "SDL/SDL_mixer.h"
#include <string>
#include <fstream>
#include <deque>
#include <algorithm>
#include <boost/lexical_cast.hpp>

#include "timer.h"

using namespace std;

//Screen attributes
const int SCREEN_WIDTH = 900;
const int SCREEN_HEIGHT = 700;
const int SCREEN_BPP = 32;

//Frame Rate
const int FRAMES_PER_SECOND = 20;

//The surfaces
SDL_Surface *background = NULL;
SDL_Surface *mainbackground = NULL;
SDL_Surface *message = NULL;
SDL_Surface *mainscreen = NULL;
SDL_Surface *escapeMessage = NULL;
SDL_Surface *redButtonMessage = NULL;
SDL_Surface *greenButtonMessage = NULL;
SDL_Surface *yellowButtonMessage = NULL;
SDL_Surface *orangeButtonMessage = NULL;
SDL_Surface *whiteButtonMessage = NULL;
SDL_Surface *creditsMessage = NULL;
SDL_Surface *creditsNames = NULL;
SDL_Surface *hiscore1 = NULL;
SDL_Surface *hiscore2 = NULL;
SDL_Surface *hiscore3 = NULL;
SDL_Surface *hiscore4 = NULL;
SDL_Surface *hiscore5 = NULL;
SDL_Surface *hiscore1a = NULL;
SDL_Surface *hiscore2a = NULL;
SDL_Surface *hiscore3a = NULL;
SDL_Surface *hiscore4a = NULL;
SDL_Surface *hiscore5a = NULL;



//The event structure
SDL_Event event;

//The font
TTF_Font *size40font = NULL;
TTF_Font *size20font = NULL;
TTF_Font *size30font = NULL;
TTF_Font *size10font = NULL;

//The color of the font
SDL_Color textColor = { 0, 0, 0 };
SDL_Color whiteColor = {255,255,255};

SDL_Surface *load_image (std::string filename)
{
  //The image that's loaded
  SDL_Surface *loadedImage = NULL;

  //The optimized surface that will be used
  SDL_Surface *optimizedImage = NULL;

  //Load the image
  loadedImage = IMG_Load (filename.c_str ());

  //If the image loaded
  if (loadedImage != NULL)
  {
	//Create an optimized surface
	optimizedImage = SDL_DisplayFormat (loadedImage);

	//Free the old surface
	SDL_FreeSurface (loadedImage);

	//If the surface was optimized
	if (optimizedImage != NULL)
	{
	  //Color key surface
	  SDL_SetColorKey (optimizedImage, SDL_SRCCOLORKEY,
					   SDL_MapRGB (optimizedImage->format, 0, 0xFF, 0xFF));
	}
  }

  //Return the optimized surface
  return optimizedImage;
}

void apply_surface (int x, int y, SDL_Surface * source,
					SDL_Surface * destination, SDL_Rect * clip = NULL)
{
  //Holds offsets
  SDL_Rect offset;

  //Get offsets
  offset.x = x;
  offset.y = y;

  //Blit
  SDL_BlitSurface (source, clip, destination, &offset);
}

bool init ()
{
  //Initialize all SDL subsystems
  if (SDL_Init (SDL_INIT_EVERYTHING) == -1)
  {
	return false;
  }

  //Set up the screen
  mainscreen =
	SDL_SetVideoMode (SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE);

  //If there was an error in setting up the screen
  if (mainscreen == NULL)
  {
	return false;
  }

  //Initialize SDL_ttf
  if (TTF_Init () == -1)
  {
	return false;
  }

  //Set the window caption
  SDL_WM_SetCaption ("Project Interface", NULL);

  //If everything initialized fine
  return true;
}

bool load_files ()
{


//***********Loading Background Image**************
  //Load the background image
  background = load_image ("ocean2.jpg");
  mainbackground = load_image ("nemo.jpg");


  //If there was a problem in loading the background
  if (background == NULL)
  {
	return false;
  }

//*************End Loading Background Image**************



//***********Loading Fonts*************
  //Open the font
  size40font = TTF_OpenFont ("AgentOrange.ttf", 40);
  size20font = TTF_OpenFont ("AgentOrange.ttf", 20);
  size30font = TTF_OpenFont ("AgentOrange.ttf", 30);
  size10font = TTF_OpenFont ("AgentOrange.ttf", 10);


  //If there was an error in loading the font
  if (size40font == NULL)
  {
	return false;
  }
  if (size20font == NULL)
  {
	return false;
  }
  if (size30font == NULL)
  {
	return false;
  }
//***********End Loading Fonts***************



  //If everything loaded fine
  return true;
}


void clean_up ()
{
  //Free the surfaces
  SDL_FreeSurface (background);


  //Close the font
  TTF_CloseFont (size20font);
  TTF_CloseFont (size30font);
  TTF_CloseFont (size40font);


  //Quit SDL_ttf
  TTF_Quit ();

  //Quit SDL
  SDL_Quit ();
}


void BubbleSort (deque < int >&intscores, int newscore)
{
  int i, j, flag = 1;			// set flag to 1 to start first pass
  int temp;						// holding variable

/*
  cout << "bubblesort function" << endl;
  cout << "before pushback" << endl;
  for(int x=0;x<5;x++)
  {
    cout << "intscores " << x << " equals " << intscores[x] << endl;
  }
*/

  intscores.push_back (newscore);	//adding new score to deque

/*
  cout << "after pushback" << endl;
  for(int x=0;x<5;x++)
  {
    cout << "intscores " << x << " equals " << intscores[x] << endl;
  }
*/

  int numLength = intscores.size();
//  cout << intscores.size() << endl;

  for (i = 1; (i <= numLength); i++)
  {
	flag = 0;
	for (j = 0; j < (numLength - 1); j++)
	{
	  if (intscores[j + 1] > intscores[j])	// ascending order simply changes to <
	  {
		//cout << "intscores[j+1] = " << intscores[j+1] << endl;
		//cout << "intscores[j] = " << intscores[j] << endl;
		temp = intscores[j];	// swap elements
		intscores[j] = intscores[j + 1];
		intscores[j + 1] = temp;
		flag = 1;				// indicates that a swap occurred.
	  }
	}
  }
/*
  cout << "after sorting" << endl;
  for(int x=0;x<5;x++)
  {
    cout << "intscores " << x << " equals " << intscores[x] << endl;
  }
*/

  intscores.pop_back ();
/*
  cout << "after pop_back" << endl;
  for(int x=0;x<5;x++)
  {
    cout << "intscores " << x << " equals " << intscores[x] << endl;
  }
*/

  return;
}

//***********main program*************

int main (int argc, char *args[])
{
  //Quit flag
  bool quit = false;

  //Background Offsets
  int bgX = 0, bgY = 0;

  //Frame rate regulator
  Timer fps;

//input stuff
  int havescoresbeeninputinitially = 0;
  int havescoresbeeninputonpage3 = 0;
  deque < string > scores;
  deque < int >intscores;

  int isthereanewscore = 0;
  int up = 0;




  //Initialize
  if (init () == false)
  {
	return 1;
  }

  //Load the files
  if (load_files () == false)
  {
	return 1;
  }


  int whichscreen = 1;

  //While the user hasn't quit
  while (quit == false)
  {

//111111111111111111111111111111111111111111111111111111111
//111111111111111111111111111111111111111111111111111111111
//111111111111111111111111111111111111111111111111111111111
//111111111111111111111111111111111111111111111111111111111
//111111111111111111111111111111111111111111111111111111111
//111111111111111111111111111111111111111111111111111111111
// SCREEN 1

	if (whichscreen == 1)
	{
	  //$$$$$$$$$$ BEGIN STEPS TO MAKE SCREEN 1 $$$$$$$$$$


	  //INITIALLY GETTING THE HIGH SCORES

	  if (havescoresbeeninputinitially == 0)
	  {
		string line;
		ifstream infile ("hi_scores.txt");
		if (infile.is_open ())	//if it opened correctly....
		{
		  while (infile.good ())
		  {
			getline (infile, line);
			scores.push_back (line);
		  }
		}
		infile.close ();

		havescoresbeeninputinitially = 1;
		scores.pop_back ();

		for (int i = 0; i < scores.size (); i++)
		{
		  intscores.push_back (atoi (scores[i].c_str ()));
		}
		if (scores.size () < 5)
		{
		  for (int i = 5 - scores.size (); i > 0; i--)
		  {
			intscores.push_back (0);
			scores.push_back ("0");
		  }
		}
	  

	  BubbleSort (intscores, up);
	  scores.clear ();

	  ofstream myfile;
	  myfile.open ("hi_scores.txt");

	  for (int i = 0; i < 5; i++)
	  {
		myfile << intscores[i] << endl;
		scores.push_back(boost::lexical_cast<string>(intscores[i]));
	  }


	  myfile.close ();


	  }
	  //RE-SAVING THE HIGH SCORES
	  if(isthereanewscore==1)
	  {
			/*
			cout<< "before newscore bubblesort..." << endl;
			for (int i =0;i<5;i++)
	  		{
			cout << "intscores "<< i << " equals " << intscores[i] << endl;
			cout << "scores " << i << " equals " << scores[i][0] << endl;
	 		}
			*/

		//sorting high scores
		BubbleSort(intscores, up);
		ofstream myfile;
		myfile.open ("hi_scores.txt");
		
		for (int i=0;i<5;i++)
		{
			/*
			cout<< "after newscore bubblesort..." << endl;
			for (int j =0;j<5;j++)
	  		{
			cout << "intscores "<< j << " equals " << intscores[j] << endl;
			cout << "scores " << j << " equals " << scores[j][0] << endl;
	 		}
			*/


			myfile << intscores[i] << endl;
			scores.push_back(boost::lexical_cast<string>(intscores[i]));
		}
		
		myfile.close();

		isthereanewscore=0;
		up =0;
		havescoresbeeninputonpage3 = 0;

	  }



	/*
	  cout << "page1" << endl;
	  for (int i =0;i<5;i++)
	  {
		cout << "intscores "<< i << " equals " << intscores[i] << endl;
		cout << "scores " << i << " equals " << scores[i][0] << endl;
	  }
	*/

/*
	  Uint32 mainscreenColor;	//needed for changing color of background
	  SDL_Rect mainscreenRect;
	  mainscreenRect.x = mainscreenRect.y = 0;
	  mainscreenRect.w = mainscreen->w;
	  mainscreenRect.h = mainscreen->h;
	  mainscreenColor = SDL_MapRGB (mainscreen->format, 20, 96, 255);
	  SDL_FillRect (mainscreen, &mainscreenRect, mainscreenColor);
*/
	  apply_surface (bgX, bgY, mainbackground, mainscreen);
	  SDL_Rect b1;				//Making a red button (B1)
	  Uint32 b1color;
	  b1.x = 50;
	  b1.y = 150;
	  b1.w = 100;
	  b1.h = 50;
	  b1color = SDL_MapRGB (mainscreen->format, 255, 0, 0);
	  SDL_FillRect (mainscreen, &b1, b1color);

	  SDL_Rect b2;				//Making a yellow button (B2)
	  Uint32 b2color;
	  b2.x = 50;
	  b2.y = 300;
	  b2.w = 100;
	  b2.h = 50;
	  b2color = SDL_MapRGB (mainscreen->format, 255, 255, 0);
	  SDL_FillRect (mainscreen, &b2, b2color);

	  SDL_Rect b3;				//Making a white button (B3)
	  Uint32 b3color;
	  b3.x = 50;
	  b3.y = 450;
	  b3.w = 100;
	  b3.h = 50;
	  b3color = SDL_MapRGB (mainscreen->format, 255, 255, 255);
	  SDL_FillRect (mainscreen, &b3, b3color);


	  //*******adding a message to screen*********
	  //Render the text
	  message =
		TTF_RenderText_Solid (size40font, "Welcome to the Game!", whiteColor);
	  redButtonMessage =
		TTF_RenderText_Solid (size30font, "Play", whiteColor);
	  yellowButtonMessage =
		TTF_RenderText_Solid (size30font, "Hi-Scores", whiteColor);
	  whiteButtonMessage =
		TTF_RenderText_Solid (size30font, "Credits", whiteColor);

	  //If there was an error in rendering the text
	  if (message == NULL)
	  {
		return 1;
	  }
	  if (redButtonMessage == NULL)
	  {
		return 1;
	  }
	  if (yellowButtonMessage == NULL)
	  {
		return 1;
	  }
	  if (whiteButtonMessage == NULL)
	  {
		return 1;
	  }


	  //Show the message on the screen
	  apply_surface ((SCREEN_WIDTH - message->w) / 2, 18, message,
					 mainscreen);
	  apply_surface (175, 162.5, redButtonMessage, mainscreen);
	  apply_surface (175, 312.5, yellowButtonMessage, mainscreen);
	  apply_surface (175, 462.5, whiteButtonMessage, mainscreen);

	  //Free the message
	  SDL_FreeSurface (message);
	  SDL_FreeSurface (redButtonMessage);
	  SDL_FreeSurface (yellowButtonMessage);
	  SDL_FreeSurface (whiteButtonMessage);
	  //*******end adding a message to screen*********

	  if (SDL_Flip (mainscreen) == -1)
	  {
		return 1;
	  }
	  //$$$$$$$$ END STEPS TO MAKE SCREEN 1 $$$$$$$$$$


	  //$$$$$$$$ HANDLING EVENTS OF SCREEN 1 $$$$$$$$$

	  //While there's events to handle
	  while (SDL_PollEvent (&event))
	  {
		//The mouse coordinates
		int x = 0, y = 0;

		//If a mouse button was pressed
		if (event.type == SDL_MOUSEBUTTONDOWN)
		{

		  //If the left mouse button was pressed
		  if (event.button.button == SDL_BUTTON_LEFT)
		  {
			//Get the mouse offsets
			x = event.button.x;
			y = event.button.y;

			//If the mouse is over the red button
			if ((x > b1.x) && (x < b1.x + b1.w) && (y > b1.y)
				&& (y < b1.y + b1.h))
			{
			  cout << "The red button has been clicked" << endl;
			  cout << "whichscreen is now 2" << endl;
			  whichscreen = 2;

			}

			//If the mouse is over the yellow button
			if ((x > b2.x) && (x < b2.x + b2.w) && (y > b2.y)
				&& (y < b2.y + b2.h))
			{
			  cout << "The yellow button has been clicked" << endl;
			  cout << "whichscreen is now 3" << endl;
			  whichscreen = 3;
			}

			//If the mouse is over the white button
			if ((x > b3.x) && (x < b3.x + b3.w) && (y > b3.y)
				&& (y < b3.y + b3.h))
			{
			  cout << "The white button has been clicked" << endl;
			  cout << "whichscreen is now 4" << endl;
			  whichscreen = 4;
			}
		  }
		}
		//If the user has Xed out the window
		if (event.type == SDL_QUIT)
		{
		  //Quit the program
		  quit = true;
		}


	  }

	  //$$$$$$$$ END HANDLING EVENTS OF SCREEN 1 $$$$$$$$$

	}

//222222222222222222222222222222222222222222222222222222222
//222222222222222222222222222222222222222222222222222222222
//222222222222222222222222222222222222222222222222222222222
//222222222222222222222222222222222222222222222222222222222
//222222222222222222222222222222222222222222222222222222222
//222222222222222222222222222222222222222222222222222222222
// SCREEN 2

	if (whichscreen == 2)
	{

	  //$$$$$$$$$$ BEGIN STEPS TO MAKE SCREEN 2 / ACTUAL GAME $$$$$$$$$$


	/*
	  SDL_Rect b4;				//making a black button (B4)
	  Uint32 b4color;
	  b4.x = 50;
	  b4.y = 600;
	  b4.w = 100;
	  b4.h = 50;
	  b4color = SDL_MapRGB (mainscreen->format, 255, 255, 255);
	  SDL_FillRect (mainscreen, &b4, b4color);	//showing button
	*/


	  //*******adding a message to screen*********
	  //Render the text
	  message = TTF_RenderText_Solid (size40font, "Welcome to Page 2!", whiteColor);
	  escapeMessage =
		TTF_RenderText_Solid (size10font, "Press Escape to Quit", whiteColor);

	  //If there was an error in rendering the text
	  if (message == NULL)
	  {
		return 1;
	  }
	  if (escapeMessage == NULL)
	  {
		return 1;
	  }

	  //*******end adding a message to screen*********

	  //$$$$$$$$ END STEPS TO MAKE SCREEN 2 $$$$$$$$$$

	  //$$$$$$$$ HANDLING EVENTS OF SCREEN 2 $$$$$$$$$


	  while (whichscreen == 2)
	  {
		fps.start ();

		//While there's events to handle
		while (SDL_PollEvent (&event))
		{
		  //The mouse coordinates
		  int x = 0, y = 0;


		/*
		  //If a mouse button was pressed
		  if (event.type == SDL_MOUSEBUTTONDOWN)
		  {

			//If the left mouse button was pressed
			if (event.button.button == SDL_BUTTON_LEFT)
			{
			  //Get the mouse offsets
			  x = event.button.x;
			  y = event.button.y;

			  //If the mouse is over the red button
			  if ((x > b4.x) && (x < b4.x + b4.w) && (y > b4.y)
				  && (y < b4.y + b4.h))
			  {
				cout << "The black button has been clicked" << endl;
				cout << "whichscreen is now 1" << endl;
				whichscreen = 1;
				isthereanewscore=1;
			  }
			}
		  }
			*/


		  //if up button was pressed
		  if (event.type == SDL_KEYDOWN)
		  {

			switch (event.key.keysym.sym)
			{
			case SDLK_UP:
			  cout << "up button pressed" << endl;
			  up++;
			  break;
			case SDLK_ESCAPE:
			  cout << "escape pressed" << endl;
			  whichscreen = 1;
			  isthereanewscore = 1;
			  break;
			}

		  }
		  //If the user has Xed out the window
		  if (event.type == SDL_QUIT)
		  {
			//Quit the program
			quit = true;
			cout << "exited the game" << endl;
		  }
		}

		bgX -= 2;

		//If the background has gone too far
		if (bgX <= -background->w)
		{
		  //Reset the offset
		  bgX = 0;
		}

		//Show the background
		apply_surface (bgX, bgY, background, mainscreen);
		apply_surface (bgX + background->w, bgY, background, mainscreen);

		//SDL_FillRect (mainscreen, &b4, b4color);

		//Showing the messages on the screen
		apply_surface ((SCREEN_WIDTH - message->w) / 2, 18, message,
					   mainscreen);
		apply_surface ((SCREEN_WIDTH - escapeMessage->w) / 2, 650, escapeMessage, mainscreen);

		//Update the screen
		if (SDL_Flip (mainscreen) == -1)
		{
		  return 1;
		}

		//Cap the frame rate
		if (fps.get_ticks () < 1000 / FRAMES_PER_SECOND)
		{
		  SDL_Delay ((1000 / FRAMES_PER_SECOND) - fps.get_ticks ());
		}



	  }
	  //$$$$$$$$ END HANDLING EVENTS OF SCREEN 2 $$$$$$$$$
	}


//333333333333333333333333333333333333333333333333333333333
//333333333333333333333333333333333333333333333333333333333
//333333333333333333333333333333333333333333333333333333333
//333333333333333333333333333333333333333333333333333333333
//333333333333333333333333333333333333333333333333333333333
//333333333333333333333333333333333333333333333333333333333
// SCREEN 3

	if (whichscreen == 3)
	{

	  //$$$$$$$$$$ BEGIN STEPS TO MAKE SCREEN 3 / SAVED GAME RESULTS $$$$$$$$$$

	  Uint32 mainscreenColor;	//needed for changing color of background
	  SDL_Rect mainscreenRect;
	  mainscreenRect.x = mainscreenRect.y = 0;
	  mainscreenRect.w = mainscreen->w;
	  mainscreenRect.h = mainscreen->h;
	  mainscreenColor = SDL_MapRGB (mainscreen->format, 255, 255, 0);
	  SDL_FillRect (mainscreen, &mainscreenRect, mainscreenColor);

	  SDL_Rect b5;				//making a green button (B5)
	  Uint32 b5color;
	  b5.x = 50;
	  b5.y = 590.5;
	  b5.w = 100;
	  b5.h = 50;
	  b5color = SDL_MapRGB (mainscreen->format, 0, 153, 76);
	  SDL_FillRect (mainscreen, &b5, b5color);

	  //*******adding a message to screen*********
	  //Render the text
	  message =
		TTF_RenderText_Solid (size40font, "HI SCORES",
							  textColor);
	  greenButtonMessage =
		TTF_RenderText_Solid (size30font, "Go back to the home page", textColor);

	  //If there was an error in rendering the text
	  if (message == NULL)
	  {
		return 1;
	  }
	  if (greenButtonMessage == NULL)
	  {
		return 1;
	  }

	  //Show the message on the screen
	  apply_surface ((SCREEN_WIDTH - message->w) / 2, 18, message,
					 mainscreen);
	  apply_surface (175, 600, greenButtonMessage, mainscreen);

	  //Free the message
	  SDL_FreeSurface (message);
	  SDL_FreeSurface (greenButtonMessage);
	  //*******end adding a message to screen*********


	  //*******begin getting hi-scores from file*********
	  if (havescoresbeeninputonpage3 == 0)
	  {
		scores.clear();
		string line;
		ifstream infile ("hi_scores.txt");
		if (infile.is_open ())	//if it opened correctly....
		{
		  while (infile.good ())
		  {
			getline (infile, line);
			scores.push_back (line);
		  }
		}
		infile.close ();
		havescoresbeeninputonpage3 == 1;

		scores.pop_back ();


	/*
	  for (int i =0;i<5;i++)
	  {
		cout << "intscores "<< i << " equals " << intscores[i] << endl;
		cout << "scores " << i << " equals " << scores[i][0] << endl;
	  }
	*/

		intscores.clear();
		for (int i = 0; i < scores.size (); i++)
		{
		  intscores.push_back (atoi (scores[i].c_str ()));
		 // cout << "added " << atoi (scores[i].c_str()) << endl;
		}

		//makes sure that there are at least 5 scores accounted for
		if (scores.size () < 5)
		{
		  for (int i = 5 - scores.size (); i > 0; i--)
		  {
			intscores.push_back (0);
			scores.push_back ("0");
		  }
		}
	  }
	  //********end getting hi-scores from file***********

	  //********displaying hi-scores on screen************

	  hiscore1 = TTF_RenderText_Solid (size30font, &scores[0][0], textColor);
	  hiscore2 = TTF_RenderText_Solid (size30font, &scores[1][0], textColor);
	  hiscore3 = TTF_RenderText_Solid (size30font, &scores[2][0], textColor);
	  hiscore4 = TTF_RenderText_Solid (size30font, &scores[3][0], textColor);
	  hiscore5 = TTF_RenderText_Solid (size30font, &scores[4][0], textColor);
	  hiscore1a = TTF_RenderText_Solid (size30font, "1st Place:", textColor);
	  hiscore2a = TTF_RenderText_Solid (size30font, "2nd Place:", textColor);
	  hiscore3a = TTF_RenderText_Solid (size30font, "3rd Place:", textColor);
	  hiscore4a = TTF_RenderText_Solid (size30font, "4th Place:", textColor);
	  hiscore5a = TTF_RenderText_Solid (size30font, "5th Place:", textColor);

	  apply_surface (200, 150, hiscore1a, mainscreen);
	  apply_surface (200, 225, hiscore2a, mainscreen);
	  apply_surface (200, 300, hiscore3a, mainscreen);
	  apply_surface (200, 375, hiscore4a, mainscreen);
	  apply_surface (200, 450, hiscore5a, mainscreen);
	  apply_surface (500, 150, hiscore1, mainscreen);
	  apply_surface (500, 225, hiscore2, mainscreen);
	  apply_surface (500, 300, hiscore3, mainscreen);
	  apply_surface (500, 375, hiscore4, mainscreen);
	  apply_surface (500, 450, hiscore5, mainscreen);

	  //*********end displaying hi-scores on screen************


	  if (SDL_Flip (mainscreen) == -1)
	  {
		return 1;
	  }
	  //$$$$$$$$ END STEPS TO MAKE SCREEN 3 $$$$$$$$$$

	  //$$$$$$$$ HANDLING EVENTS OF SCREEN 3 $$$$$$$$$

	  //While there's events to handle
	  while (SDL_PollEvent (&event))
	  {
		//The mouse coordinates
		int x = 0, y = 0;


		//If a mouse button was pressed
		if (event.type == SDL_MOUSEBUTTONDOWN)
		{

		  //If the left mouse button was pressed
		  if (event.button.button == SDL_BUTTON_LEFT)
		  {
			//Get the mouse offsets
			x = event.button.x;
			y = event.button.y;

			//If the mouse is over the green button
			if ((x > b5.x) && (x < b5.x + b5.w) && (y > b5.y)
				&& (y < b5.y + b5.h))
			{
			  cout << "The green button has been clicked" << endl;
			  cout << "whichscreen is now 1" << endl;
			  whichscreen = 1;
			}
		  }
		}



		//If the user has Xed out the window
		if (event.type == SDL_QUIT)
		{
		  //Quit the program
		  quit = true;
		}
	  }
	  //$$$$$$$$ END HANDLING EVENTS OF SCREEN 3 $$$$$$$$$
	}

//444444444444444444444444444444444444444444444444444444444
//444444444444444444444444444444444444444444444444444444444
//444444444444444444444444444444444444444444444444444444444
//444444444444444444444444444444444444444444444444444444444
//444444444444444444444444444444444444444444444444444444444
//444444444444444444444444444444444444444444444444444444444
// SCREEN 4


	if (whichscreen == 4)
	{

	  //$$$$$$$$$$ BEGIN STEPS TO MAKE CREDITS PAGE $$$$$$$$$$

	  Uint32 mainscreenColor;	//needed for changing color of background
	  SDL_Rect mainscreenRect;
	  mainscreenRect.x = mainscreenRect.y = 0;
	  mainscreenRect.w = mainscreen->w;
	  mainscreenRect.h = mainscreen->h;
	  mainscreenColor = SDL_MapRGB (mainscreen->format, 255, 255, 255);
	  SDL_FillRect (mainscreen, &mainscreenRect, mainscreenColor);

	  SDL_Rect b6;				//making an orange button (B6)
	  Uint32 b6color;
	  b6.x = 50;
	  b6.y = 600;
	  b6.w = 100;
	  b6.h = 50;
	  b6color = SDL_MapRGB (mainscreen->format, 255, 128, 0);
	  SDL_FillRect (mainscreen, &b6, b6color);

	  //*******adding a message to screen*********
	  //Render the text
	  message =
		TTF_RenderText_Solid (size30font, "Credits",
							  textColor);
	  orangeButtonMessage =
		TTF_RenderText_Solid (size30font, "Go back to the home page", textColor);
	  creditsMessage =
		TTF_RenderText_Solid (size30font,"This project was made by:", textColor);
	  creditsNames = TTF_RenderText_Solid (size30font,"Laura Cronin & Adam Zebrowski", textColor);

	  //If there was an error in rendering the text
	  if (message == NULL)
	  {
		return 1;
	  }
	  if (orangeButtonMessage == NULL)
	  {
		return 1;
	  }
	  if (creditsMessage == NULL)
	  {
		return 1;
	  }

	  //Show the message on the screen
	  apply_surface ((SCREEN_WIDTH - message->w) / 2, 18, message,
					 mainscreen);
	  apply_surface (200, 612.5, orangeButtonMessage, mainscreen);
	  apply_surface ((SCREEN_WIDTH - creditsMessage->w) / 2, 200,
					 creditsMessage, mainscreen);
	  apply_surface ((SCREEN_WIDTH - creditsNames->w) /2, 300, creditsNames, mainscreen);

	  //Free the message
	  SDL_FreeSurface (message);
	  SDL_FreeSurface (orangeButtonMessage);
	  SDL_FreeSurface (creditsMessage);
	  //*******end adding a message to screen*********

	  if (SDL_Flip (mainscreen) == -1)
	  {
		return 1;
	  }
	  //$$$$$$$$ END STEPS TO MAKE CREDITS PAGE $$$$$$$$$$

	  //$$$$$$$$ HANDLING EVENTS OF CREDITS PAGE $$$$$$$$$

	  //While there's events to handle
	  while (SDL_PollEvent (&event))
	  {
		//The mouse coordinates
		int x = 0, y = 0;

		//If a mouse button was pressed
		if (event.type == SDL_MOUSEBUTTONDOWN)
		{

		  //If the left mouse button was pressed
		  if (event.button.button == SDL_BUTTON_LEFT)
		  {
			//Get the mouse offsets
			x = event.button.x;
			y = event.button.y;

			//If the mouse is over the green button
			if ((x > b6.x) && (x < b6.x + b6.w) && (y > b6.y)
				&& (y < b6.y + b6.h))
			{
			  cout << "The green button has been clicked" << endl;
			  cout << "whichscreen is now 1" << endl;
			  whichscreen = 1;
			}
		  }
		}
		//If the user has Xed out the window
		if (event.type == SDL_QUIT)
		{
		  //Quit the program
		  quit = true;
		}
	  }
	  //$$$$$$$$ END HANDLING EVENTS OF CREDITS PAGE $$$$$$$$$
	}
  }








  return 0;
}
